import { _decorator, Camera, Component, EventTouch, find, Node, Sprite, tween, v3, Vec3 } from 'cc';
import { BManager } from '../entity/BManager';
import { VillageController } from './VillageController';
import { Space } from './Space';
import { PrefabEnum } from '../common/PrefabEnum';
import { UIManager } from '../common/UIManager';
import { SpaceBtn } from './SpaceBtn';
import { Building } from '../entity/Buildings';
import { EventManager } from '../event/EventManager';
import VillageApi from '../api/VillageApi';
import { UserInfo } from '../common/UserInfo';
import { VillageCamera } from './VillageCamera';
const { ccclass, property } = _decorator;

@ccclass('Wall')
export class Wall extends Space {
    @property(Sprite)
    wallImage:Sprite
  
    @property(Node)
    door: Node
    @property(Node)
    timeParent:Node

    private uid: number = 40

  

    start() {
       
        EventManager.instance.on(EventManager.EventType.WallClick, this.onTouch, this)


        EventManager.instance.on(EventManager.EventType.GetVillageDetail, this.init, this)
        EventManager.instance.on(EventManager.EventType.BuildWall,this.buildWall, this)
        EventManager.instance.on(EventManager.EventType.RemoveWall, this.removeWall, this)
    }

    init() {
        
        let bm = BManager.getBManage(this.uid)
        this.buildData = Building.createBuildingByBManager(bm)
        if(this.buildData.gid == 0){
            this.buildData.name = '城墙'
            this.wallImage.node.active = false
        } else {
            this.wallImage.node.active = true
        }
        
        this.setBuildingState()
    }

    async onTouch(data:{node:Node, eventTouch: EventTouch}) {
        console.log('wall touch...')
        let ui = await UIManager.instance.openUI(PrefabEnum.SpaceBtn)
        ui.getComponent(SpaceBtn).setData(this, data.eventTouch)
    }

    /**
     * 建造，
     * @param id 建筑的id
     */
    async buildWall(uid:number) {
        // 人族城墙:31, 兽族城墙:32, 精灵城墙:33
        // const raceName = [ "人族", "兽族", "精灵", "野兽" ];
        //const raceType = { Rome: 0, Teuton: 1, Gaul: 2, NATURE: 3 };

        if(this.uid != uid){
            return
        }

        let gidList = [31, 32, 33]

        await UIManager.instance.showLoading()

        let res = await VillageApi.buildingOper({
            gid: gidList[UserInfo.instance.race],
            uid: this.uid
        })

        await UserInfo.instance.refreshVillageDetail()
        UIManager.instance.closeLoading()

    

        this.moveToDoor()
    }

    async removeWall(uid:number) {
      
        if(this.uid != uid){
            return
        }
       
        await UIManager.instance.showLoading()
        let res = await VillageApi.destroyBuilding(this.uid)
        console.log(`拆除${this.uid}:`, res)
        UserInfo.instance.refreshVillageDetail()
        UIManager.instance.closeLoading()

        this.moveToDoor()
       
    }

    moveToDoor(){
        let nodePos = this.door.worldPosition
        let camera = VillageCamera.instance.node
        let cameraPos = camera.worldPosition
       
        tween(camera)
        .to(1, {worldPosition: new Vec3(nodePos.x, nodePos.y, cameraPos.z)})
        .start()
    }


}


